package com.waypolice.common.utils;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * session失效后跳转登录页面(此处代码暂时在测试中,需多次测验失效)
 * @author wr
 *
 */
public class SessionFilter implements HandlerInterceptor {

	private static final String LOGIN_URL="/sessionrun.jsp";
	
	/**
	 * 在业务处理器处理请求之前被调用
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		HttpSession session=request.getSession(true); 
	    //session中获取用户名信息 
		
	    Object obj = session.getAttribute("USERSTRONE_SESSION"); 
	   if (obj==null||"".equals(obj.toString())) {
	      response.sendRedirect(request.getSession().getServletContext().getContextPath()+LOGIN_URL);
	         return false;
	      }
	    return true;
	}

	/**
	 * 在业务处理器处理请求执行完成后,生成视图之前执行
	 */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	/**
	 * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}


}
